/*
* Copyright (c) 2007, Nubo Technologies
* 
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.nubotech.gwt.oss.browser.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.nubotech.gwt.oss.browser.client.State;
import com.nubotech.gwt.oss.client.ObjectHolder;
import com.nubotech.gwt.oss.client.OnlineStorageService;
import com.nubotech.gwt.oss.client.OnlineStorageServiceFactory;
import com.nubotech.gwt.oss.client.auth.AuthenticationManager;
import com.nubotech.gwt.oss.client.auth.Credential;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author jonnakkerud
 */
public class EditContentPanel extends Composite {
    private DockPanel main;
    private State state;
    
    private TextBox key;
    private TextArea value;    
    private ListBox accessList;
    private MetaDataEditor metaDataEditor;
    
    public EditContentPanel(State state) {
        this.state = state;
        initWidget(main = new DockPanel());
        initGui();
    }
    
    private void initGui() {
      
        FlexTable table = new FlexTable();
        table.setCellSpacing(5);
        main.add(table, DockPanel.CENTER);
        
        // bucket
        table.setText(0, 0, "Bucket:");
        table.setText(0, 1, this.state.getCurrentBucket().getName());        
        
        // key
        table.setText(1, 0, "Key:");
        key = new TextBox();
        key.setVisibleLength(50);
        table.setWidget(1, 1, key);        
        
        // value
        table.setText(2,0,"Value:");
        value = new TextArea();
        value.setVisibleLines(10);
        value.setCharacterWidth(100);
        table.setWidget(2,1,value);
        
        table.setText(3,0, "Access Policy:");
        accessList = new ListBox();
        accessList.addItem("Private", "0");
        accessList.addItem("Public Read", "1");
        accessList.addItem("Public Read Write", "2");
        accessList.addItem("Authenticated Read", "3");
        table.setWidget(3,1, accessList);
        
        // edit meta data
        metaDataEditor = new MetaDataEditor();
        main.add(metaDataEditor, DockPanel.SOUTH);
    }
    
    public void submit() {
        // get the key and the value and the bucket
        AuthenticationManager mgr = AuthenticationManager.instance();
        Credential c = mgr.getLogin();
        
        OnlineStorageService fs = OnlineStorageServiceFactory.getService(c);
        
        ObjectHolder content = new ObjectHolder();
        content.setKey(key.getText());
        content.setData(value.getText());
        int policy = Integer.parseInt(accessList.getValue(accessList.getSelectedIndex()));
        content.setAccessPolicy(policy);
        content.setMetaData(metaDataEditor.getMetaData());
        fs.putObject(state.getCurrentBucket().getName(), content);
    }
    
    class MetaDataEditor extends Composite {
        private DockPanel main;
        private FlexTable metaTable;
        
        public MetaDataEditor() {
            initWidget(main = new DockPanel());
            initGui();
        }
        
        private void initGui() {
            Button addButton = new Button("Add MetaData");
            addButton.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    addRow();
                }
            });
            main.add(addButton, DockPanel.NORTH);
            
            metaTable = new FlexTable();
            main.add(metaTable, DockPanel.CENTER);
        }
        
        private void addRow() {
            final int row = metaTable.getRowCount();
            metaTable.setWidget(row, 0, new TextBox());
            metaTable.setWidget(row, 1, new TextBox());
            
            Button removeButton = new Button("Remove");
            removeButton.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    metaTable.removeRow(row);
                }
            });
            
            metaTable.setWidget(row, 2, removeButton);
        }
        
        public Map getMetaData() {
            Map data = new HashMap();
            GWT.log("row count="+metaTable.getRowCount(), null);
            for (int row = 0; row < metaTable.getRowCount(); row++) {
                TextBox name = (TextBox) metaTable.getWidget(row, 0);
                TextBox value = (TextBox) metaTable.getWidget(row, 1);
                data.put(name.getText(), value.getText());
            }
            
            return data;
        }
    }
    
}
